//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    menuType: 1,
    // 首页模块列表
    modelList: []
  },

  onLoad: function() {
    var that = this;
    this.loadHomepageConfig();
    this.getMenuConfig();
  },

  // 获取首页菜单配置
  getMenuConfig() {
    let that = this;
    let params = {
      point_url: app.server.agentApi.getWxappStyle,
    }
    app.server.getConnect(params).then(function(res) {
      console.log('========小程序样式配置========');
      that.setData({
        menuType: res.data.memberFormat
      })
      // 自定义导航栏跳转
      // if (res.data.tabbarList[0].pagePath !== app.globalData.config['SwitchTabToAgentPageUrl']){
      //   app.globalData.config['SwitchTabToAgentPageUrl'] = res.data.tabbarList[0].pagePath;
      //   wx.reLaunch({
      //     url: res.data.tabbarList[0].pagePath
      //   })
      // }
    })
  },

  // 获取首页配置
  loadHomepageConfig() {
    var that = this;
    var data = {
      point_url: app.server.agentApi.getWxappHomepage
    };
    app.server.getConnect(data).then(function(res) {
      that.setData({
        modelList: res.data
      })
      let pluginList = [];
      for (let i in res.data) {
        if (res.data[i]['id'] === 3) {
          pluginList = res.data[i]['plugUnitList'];
          break;
        }
      }
      wx.setStorageSync('pluginList', pluginList);
      app.globalData.pluginList = pluginList;
    })
  },

  toBannerUrl(e) {
    let item = e.currentTarget.dataset.item;
    if(item.path){
      wx.navigateTo({
        url: item.wxPath + (item.param?('?id='+item.param):''),
      })
    }
  },

  bindToPages(e) {
    this.getFormId(e);
    let plugItem = e.currentTarget.dataset.item || {};
    console.log(plugItem);
    let url = '';
    // 附属插件
    // if (plugItem.id === 164 || plugItem.type === 'NEWS') {
      if (plugItem.id === 164 || plugItem.type === 'NEWS') {
      // 新闻资讯
      url = '/pages/agent/plugins/news/list/index?pageName=' + (plugItem['new_name'] || plugItem['label']) + '&type_id=' + plugItem['type_id'];
    } else if (plugItem.auth_id === 165) {
      // 腾讯云助手
      url = '/pages/agent/plugins/tencentCloud/searchOrder/searchOrder?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 166) {
      // 活动报名
      url = '/pages/agent/plugins/activity/list/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 167) {
      // 通讯录
      url = '/pages/agent/plugins/telephoneBook/list/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 168) {
      // 考试培训
      url = '/pages/agent/plugins/exam/list/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 175) {
      // 商城业务
      url = '/pages/agent/plugins/business/index/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 176) {
      // 快速充值
      url = '/pages/agent/plugins/wallet/recharge/recharge?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 177) {
      // 我的钱包
      url = '/pages/agent/plugins/wallet/record/record?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 178) {
      // 订单管理
      url = '/pages/agent/plugins/business/order/order?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 179) {
      // 提交工单
      url = '/pages/agent/plugins/workOrder/index/index';
    } else if (plugItem.auth_id === 214) {
      // 我要反馈
      url = '/pages/agent/plugins/feedback/create/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 215) {
      // 服务热线
      url = '/pages/agent/plugins/enterprise/hotline/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 213) {
      // 企业展示
      url = '/pages/agent/plugins/enterprise/display/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 221) {
      // 物品申请
      url = '/pages/agent/plugins/applicationGoods/list/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    } else if (plugItem.auth_id === 252) {
      // 超级工单
      url = '/pages/agent/plugins/superWorkOrder/list/index?pageName=' + (plugItem['new_name'] || plugItem['label']);
    }
    wx.navigateTo({
      url: url
    })
  },

  // 跳转新闻详情
  toNewsInfo(e) {
    wx.navigateTo({
      url: "/pages/agent/plugins/news/details/index?id=" + e.currentTarget.dataset.id
    })
  },

  getFormId(e) {
    console.log('====addWxappFormId====');
    console.log(e.detail.formId);
    app.globalData.userInfo && app.server.getConnect({
      point_url: app.server.agentApi.addFormId,
      formId: e.detail.formId,
      id: app.globalData.userInfo.id || wx.getStorageSync('userInfo').id
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    app.userInfoReadyCallback = function() {
      // 设置顶部导航栏标题
      wx.setNavigationBarTitle({
        title: app.globalData.wxappStyle.wxappletName || wx.getStorageSync('wxappStyle').wxappleName || ''
      })
    }
  },

  onReady: function() {
    app.utils.initAgentWxappStyle(app, this);
    // app.pageOnloadInit(this, app);
    wx.setNavigationBarTitle({
      title: app.globalData.wxappStyle.wxappletName || wx.getStorageSync('wxappStyle').wxappleName || ''
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadHomepageConfig();
    // 初始化小程序样式
    app.utils.initAgentWxappStyle(app, this);
    wx.stopPullDownRefresh();
    this.setData({
      menuType: app.globalData.wxappStyle.menuType
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: that.data.shareAppMessageTitle || app.globalData['wxappStyle']['wxappletName'] || wx.getStorageSync('wxappStyle')['wxappletName'],
      path: app.utils.getCurrentPageUrlWithArgs() || 'pages/agent/homepage/index/index'
    }
  }
})